package com.adprof.demo.natives.listview;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.adprof.demo.android.R;
import com.adprof.demo.utils.ConstantsDemo;
import com.adprof.sdk.api.AdError;
import com.adprof.sdk.api.AdRequest;
import com.adprof.sdk.api.NativeAdData;
import com.adprof.sdk.api.NativeAdLoadListener;
import com.adprof.sdk.api.NativeAd;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NativeListviewDemoActivity extends AppCompatActivity {

    private static final int NORMAL_ITEM_COUNT = 8;
    private NativeAdListAdapter myAdapter;
    private NativeAd nativeAd;
    private final List<NativeAdData> mData = new ArrayList<>();
    private boolean isLoading = false;

    private ListView listView;
    private ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demo_activity_native_ad_list);
        listView = findViewById(R.id.listView);
        progressBar = findViewById(R.id.progressBar);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        initView();
    }

    private void initView() {
        myAdapter = new NativeAdListAdapter(this, mData);
        listView.setAdapter(myAdapter);

        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            int firstVisibleItem;
            private int visibleItemCount;
            private int totalItemCount;

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState == SCROLL_STATE_IDLE && !isLoading && (firstVisibleItem + visibleItemCount) >= totalItemCount) {
                    loadListAd();
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                this.firstVisibleItem = firstVisibleItem;
                this.visibleItemCount = visibleItemCount;
                this.totalItemCount = totalItemCount;
            }
        });

        loadListAd();
    }

    private void showLoading(boolean show) {
        isLoading = show;
        progressBar.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    private void loadListAd() {
        Log.d(ConstantsDemo.LOG_TAG, "-----------loadListAd-----------");
        showLoading(true);
        Map<String, Object> options = new HashMap<>();
        String adUnitID = ConstantsDemo.NATIVE_SELF_RENDER_SLOTID;

        AdRequest adRequest = new AdRequest.Builder()
                .setCodeId(adUnitID)
                .setIsExpressAd(ConstantsDemo.expressAD(adUnitID))
                .setExtOption(options).build();

        nativeAd = new NativeAd(adRequest, new NativeAdLoadListener() {
            @Override
            public void onAdError(AdError error) {
                Log.d(ConstantsDemo.LOG_TAG, "----------onAdError----------:" + error.toString() + ":" + adUnitID);
                Toast.makeText(NativeListviewDemoActivity.this, "onAdError:" + error, Toast.LENGTH_SHORT).show();
                showLoading(false);
            }

            @Override
            public void onAdLoad(List<NativeAdData> adDataList) {
                showLoading(false);
                if (nativeAd.isReady() && adDataList != null && !adDataList.isEmpty()) {
                    Log.d(ConstantsDemo.LOG_TAG, "----------onAdLoad----------:" + adDataList.size());
                    for (final NativeAdData adData : adDataList) {
                        Collections.addAll(mData, new NativeAdData[NORMAL_ITEM_COUNT]);
                        mData.add(adData);
                        Collections.addAll(mData, new NativeAdData[NORMAL_ITEM_COUNT]);
                    }

                    myAdapter.notifyDataSetChanged();
                }
            }
        });
        nativeAd.loadAd();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mData.clear();
        if (nativeAd != null) {
            nativeAd.destroyAd();
            nativeAd = null;
        }

    }

}